import React, { Component } from 'react'
import PubSub from 'pubsub-js'
import './index.css'

export default class Weather extends Component {
	state = {name:''}
	render() {
		const {name} = this.state
		return (
			<div className="weather">
				<h2>我是Weather组件,{name ? ('学校名：'+ name) : ''}</h2>
				<button onClick={this.unsub}>取消订阅消息</button>
			</div>
		)
	}
	componentDidMount(){
		//订阅消息
		this.pid = PubSub.subscribe('peiqi',(_,data)=>{
			console.log('我是Weather组件，我收到了数据：',data)
			this.setState({name:data})
		})
	}
	componentWillUnmount(){
		PubSub.unsubscribe(this.pid)
	}
	unsub = ()=>{
		PubSub.unsubscribe(this.pid)
	}
}
